/*
 * SingletonClassProvider.java
 *
 * Created on 2008/06/08 +0900
 *
 * $Revision: 33 $
 * $Date: 2008-07-27 15:31:44 +0000 (Sun, 27 Jul 2008) $
 */

package org.gikolet.framework.inject;

import org.gikolet.framework.util.Assert;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 33 $ $Date: 2008-07-27 15:31:44 +0000 (Sun, 27 Jul 2008) $
 */
public class SingletonClassProvider implements Provider {
    private Class clazz;
    private Object instance;

    public SingletonClassProvider(Class clazz) {
	Assert.isNotNull(clazz);

	this.clazz = clazz;
    }

    public Object get() {
	synchronized (Injector.class) {
	    if (this.instance == null) {
		try {
		    this.instance = this.clazz.newInstance();
		} catch (Exception e) {
		    e.printStackTrace();
		    throw new InjectException();
		}
	    }
	    return this.instance;
	}
    }
}
